home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 101-125 / scopedisk108 / unshar / src / tiny.a < prev    next >
Text File  |  1995-03-19  |  8KB  |  236 lines

  1. *:ts=8
  2. ****************************************************************************
  3. *                                                                         *
  4. * TINY.A                                 (C) Copyright Eddy Carroll 1989  *
  5. * ~~~~~~                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
  6. *                                                                         *
  7. * Replacement startup code for Lattice C V5.04. Use instead of c.o        *
  8. * This has many features stripped out to allow small utilities to have    *
  9. * as small a filesize as possible. In particular, don't call any of the    *
  10. * stdio functions.                                                        *
  11. *                                                                         *
  12. ****************************************************************************
  13.  
  14.        INCLUDE "exec/types.i"
  15.        INCLUDE "exec/alerts.i"
  16.        INCLUDE "exec/nodes.i"
  17.        INCLUDE "exec/lists.i"
  18.        INCLUDE "exec/ports.i"
  19.        INCLUDE "exec/libraries.i"
  20.        INCLUDE "exec/tasks.i"
  21.        INCLUDE "libraries/dos.i"
  22.        INCLUDE "libraries/dosextens.i"
  23.        INCLUDE "workbench/startup.i"
  24.        INCLUDE "exec/funcdef.i"
  25.        INCLUDE "exec/exec_lib.i"
  26.        INCLUDE "libraries/dos_lib.i"
  27.  
  28. MAXARGS            EQU 100     ; Maximum number of command line arguments from CLI
  29. AbsExecBase EQU 4      ; Welcome to the only fixed point in the universe
  30.  
  31. * A useful macro to let us call library routines
  32. callsys macro
  33.        CALLLIB _LVO\1
  34.        endm
  35.  
  36.        xdef    XCEXIT
  37.        xdef    exit
  38.        xref    LinkerDB
  39.        xref    _BSSBAS
  40.        xref    _BSSLEN
  41.  
  42.        csect   text,0,0,1,2            * xref's after this are 16-bit reloc
  43.        xref    main                    * Name of C program to start with.
  44.  
  45. start:
  46.        movem.l d1-d6/a0-a6,-(a7)
  47. REGSIZE EQU    (6+7)*4
  48.        lea     REGSIZE(a7),A5          * Determine old stack pointer
  49.        move.l  a0,a2                   * Save command pointer
  50.        move.l  d0,d2                   * and command length
  51.        lea     LinkerDB,a4             * Load base register
  52.  
  53.        move.l  AbsExecBase.W,a6
  54.        move.l  a6,SysBase(A4)
  55.        move.l  a7,_StackPtr(A4)        * Save stack ptr
  56.  
  57.        suba.l  a1,a1
  58.        callsys FindTask                * Find out our task ID
  59.        move.l  d0,a3
  60.  
  61.        move.l  a5,D0                   * get top of stack
  62.        sub.l   4(a5),D0                * compute bottom
  63.        add.l   #128,D0                 * allow for parms overflow
  64.        move.l  D0,_base(A4)            * save for stack checking
  65.  
  66.        lea     DOSName(A4),A1
  67.        moveq.l #0,D0
  68.        callsys OpenLibrary
  69.        move.l  D0,DOSBase(A4)
  70.        bne     getcom
  71. noDOS:
  72.        moveq.l #100,d0
  73.        bra     exit2
  74.  
  75. *------ find command name:
  76. getcom:
  77.        move.l  pr_CLI(a3),a0
  78.        add.l   a0,a0
  79.        add.l   a0,a0
  80.        move.l  cli_CommandName(a0),a1
  81.        add.l   a1,a1
  82.        add.l   a1,a1
  83.  
  84. *------ collect parameters:
  85.        move.l  d2,d0                   * get command line length
  86.        moveq.l #0,d1
  87.        move.b  (a1)+,d1
  88.        move.l  a1,_ProgramName(A4)
  89.        add.l   d1,d0                   * add length of command name
  90.        addq.l  #1,d0                   * allow for space after command
  91.  
  92.        clr.w   -(A7)                   * set null terminator for command line
  93.        addq.l  #1,D0                   * force to even number of bytes
  94.        andi.w  #$fffe,D0               * (round up)
  95.        sub.l   D0,A7                   * make room on stack for command line
  96.        subq.l  #2,D0
  97.        clr.w   0(A7,D0)
  98.  
  99. *------ copy command line onto stack
  100.        move.l  d2,d0                   * get command line length
  101.        subq.l  #1,d0
  102.        add.l   d1,d2
  103.  
  104. copy_line:
  105.        move.b  0(A2,D0.W),0(A7,D2.W)   * copy command line to stack
  106.        subq.l  #1,d2
  107.        dbf     d0,copy_line
  108.        move.b  #' ',0(a7,d2.w)         * add space between command and parms
  109.        subq.l  #1,d2
  110.  
  111. copy_cmd:
  112.        move.b  0(a1,d2.w),0(a7,d2.w)   * copy command name to stack
  113.        dbf     d2,copy_cmd
  114.        move.l  a7,a1                   * Get pointer to new command line
  115.  
  116.        sub.l   #(MAXARGS*4),a7         * Reserve space for argv[]
  117.        move.l  a7,a2                   * Initialise base into array
  118.        move.l  a2,a3                   * Save base of argv
  119.        moveq   #0,d2                   * Initialise argc
  120.  
  121. *
  122. * From here on down, A1 is pointer into command line
  123. *
  124. build_argv:
  125.        bsr.s   getnext                 * Read next character from line
  126.        bcs.s   doquote                 * If quote, handle
  127.        beq.s   build_argv              * If white space, skip over it
  128.  
  129.        lea     -1(a1),a0               * Get address of this parameter
  130.        bsr.s   bumpargv                * Store it to argv[] array
  131. build_2:
  132.        bsr.s   getnext                 * Get next character
  133.        bne.s   build_2                 * If not white space, keep looking
  134.        clr.b   -1(a1)                  * Zero-terminate current argument
  135.        bra.s   build_argv              * And go back to get next argument
  136.  
  137. doquote:
  138.        move.l  a1,a0                   * Get pointer to this argument
  139.        bsr.s   bumpargv                * Output it to argv[]
  140. quote_2:
  141.        bsr.s   getnext                 * Get next character
  142.        bcc.s   quote_2                 * If not quote, keep looking
  143.        clr.b   -1(a1)                  * Zero-terminate current argument
  144. quote_3:
  145.        bsr.s   getnext                 * Get next character
  146.        bne.s   quote_3                 * Skip until space reached
  147.        beq.s   build_argv              * Go back and read next argument
  148.  
  149. bumpargv:
  150.        move.l  a0,(a2)+                * Output ptr to current argument
  151.        addq    #1,d2                   * Increment argc
  152.        cmpi    #MAXARGS,d2             * Used up all our arguments yet?
  153.        bls.s   qrts                    * If not, then return
  154.        moveq   #110,d0                 * Else set return code
  155.        bra.s   exit2                   * And exit
  156.  
  157. *
  158. * Reads next character from command line. If zero, never returns, but
  159. * drops into call to main. Else, returns, with C=1 if character is quote,
  160. * Z=1 if character is white space.
  161. *
  162. getnext:
  163.        move.b  (a1)+,d0                * Get character from command line
  164.        beq.s   get_2                   * Exit if end of line
  165.        cmp.b   #34,d0                  * Check if quote
  166.        beq.s   isquote                 *
  167.        cmp.b   #32,d0                  * Check if space
  168.        beq.s   isspace                 *
  169.        cmp.b   #9,d0                   * Or tab
  170.        beq.s   isspace                 *
  171.        cmp.b   #10,d0                  * Or end of line
  172. isspace:
  173.        andi    #$1E,ccr                * Clear carry flag, retaining Z
  174. qrts   rts
  175.  
  176. isquote:
  177.        ori     #1,ccr                  * Set carry flag
  178.        andi    #$FB,ccr                * Clear zero flag
  179.        rts                             * And return
  180.  
  181. get_2:
  182.        move.l  a3,-(a7)                * Push argv onto stack
  183.        move.l  d2,-(a7)                * Push argc onto stack
  184.  
  185.        lea     _BSSBAS,a3              * get base of BSS
  186.        moveq   #0,d1
  187.        move.l  #_BSSLEN,d0             * get length of BSS in longwords
  188.        bra.s   clr_lp                  * and clear for length given
  189. clr_bss move.l d1,(a3)+
  190. clr_lp dbf     d0,clr_bss
  191.  
  192. domain:
  193.        jsr     main(PC)                * Call main(argc,argv)
  194.        moveq.l #0,d0                   * Set successful status
  195.        bra.s   exit2
  196.  
  197. exit:
  198. _exit:
  199. XCEXIT:
  200.        move.l  4(SP),d0                * Extract return code
  201. exit2:
  202.        move.l  d0,-(a7)
  203.        move.l  AbsExecBase.W,a6
  204.        move.l  DOSBase(A4),a1
  205.        callsys CloseLibrary            * Close Dos library
  206.  
  207. *------ this rts sends us back to DOS:
  208. exitToDOS:
  209.        MOVE.L  (A7)+,D0
  210.        movea.l _StackPtr(a4),SP        * Restore stack ptr
  211.        movem.l (a7)+,d1-d6/a0-a6
  212.        rts
  213.  
  214. *-----------------------------------------------------------------------
  215. * Global definitions
  216. *
  217.        csect   __MERGED,1,,2,2
  218.  
  219.        xdef    NULL,SysBase,LoadAddress,DOSBase
  220.        xdef    _oserr,_OSERR,_ONBREAK
  221.        xdef    _ProgramName,_StackPtr,_base
  222.  
  223. NULL          dc.l    0
  224. _base         dc.l    0
  225. _oserr        equ     *
  226. _OSERR        dc.l    0
  227. _ONBREAK       dc.l    0
  228. SysBase        dc.l    0
  229. LoadAddress    dc.l    0
  230. _StackPtr      dc.l    0
  231. DOSBase        dc.l    0
  232. _ProgramName   dc.l    0
  233. DOSName        dc.b    'dos.library',0
  234.  
  235.        END
  236.